package com.hrms.service.impl;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.dao.*;
import com.hrms.pojo.BasePage;
import com.hrms.pojo.ContractManage;
import com.hrms.pojo.EmpInformation;
import com.hrms.pojo.FileManage;
import com.hrms.service.ContractManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 员工合同 服务实现类
 *
 * @author LK
 * @since 2021-10-19
 */
@Service
public class ContractManageServiceImpl extends BasePage implements ContractManageService {
    @Autowired(required = false)
    ContractManageMapper contractManageMapper;
    @Autowired(required = false)
    FileManageMapper fileManageMapper;
    @Autowired(required = false)
    EmpInformationMapper empInformationMapper;
    @Autowired(required = false)
    PersonnelTransferMapper personnelTransferMapper;
    @Autowired(required = false)
    AttenManageMapper attenManageMapper;
    @Autowired(required = false)
    SalaryInfoMapper salaryInfoMapper;
    @Override
    public HashMap<String, Object> add(ContractManage contractManage) {
        HashMap<String,Object> map=new HashMap<>();
        //新增
        int num=contractManageMapper.insert(contractManage);
        if (num>0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> select(ContractManage contractManage) {
        HashMap<String,Object> map=new HashMap<>();
        //定义分页对象
        Page<ContractManage> page=new Page<>(contractManage.getPage(),contractManage.getRow());
        //条件构造器
        QueryWrapper<ContractManage> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc(contractManage.getSort()==0,"contract_begin_date");
        queryWrapper.orderByDesc(contractManage.getSort()==1,"contract_begin_date");
        //模糊查询
        queryWrapper.eq(contractManage.getStaffId()!=null,"staff_id",contractManage.getStaffId());
        List<Integer> ids=null;
        if (contractManage.getRemarks()!=null){
            ids= empInformationMapper.selectIdByStaffName(contractManage.getRemarks());
        }
        if(ids.size()>0&ids!=null){
                queryWrapper.in("staff_id",ids);
        }


        //查询数据库
        Page<ContractManage> aList= contractManageMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",aList.getRecords());
        //总页数
        map.put("totalPage",aList.getPages());
        //总条数
        map.put("total",aList.getTotal());
        //当前页
        /*================*/


            map.put("curPage",contractManage.getPage());

       /*============*/
        if(contractManage.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",contractManage.getPage()-1);

        }
        if(contractManage.getPage()==aList.getPages()){
            //下一页
            map.put("nextPage",aList.getPages());

        }else{
            map.put("nextPage",contractManage.getPage()+1);

        }
        //每页显示条数
        map.put("row",contractManage.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> update(ContractManage contractManage) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //修改
        int num = contractManageMapper.updateById(contractManage);
        if(num>0){
            map.put("info","修改成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除

        int num1 = fileManageMapper.deleteByStaffId(a);
        int num2 = contractManageMapper.deleteByStaffId(a);
        int num3 = personnelTransferMapper.deleteByStaffId(a);
        int num4 = empInformationMapper.deleteByStaffId(a);
        int num5 = attenManageMapper.deleteByStaffId(a);
        int num6 = salaryInfoMapper.deleteByStaffId(a);
        if((num1>0)||(num2>0)||(num3>0)||(num4>0)||(num5>0)||(num6>0)){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }



    //通过名字查询
    @Override
    public HashMap<String, Object> selectByName(String name) {
        List<ContractManage> contractManages = contractManageMapper.selectByName(name);
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (contractManages.size()>0){
            map.put("list",contractManages);
            map.put("info","查询成功");
        }else {
            map.put("info","查询失败");
        }


        return map;
    }

    @Override
    public void excelExport(HttpServletResponse response) throws IOException {
        List<ContractManage> list = contractManageMapper.selUserMapper();
        String fileName = "合同名单";
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xls");
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS,true);
        Sheet sheet = new Sheet(1,0,ContractManage.class);
        //设置自适应宽度
        sheet.setAutoWidth(Boolean.TRUE);
        sheet.setSheetName("合同名单");
        writer.write(list,sheet);
        writer.finish();
        out.flush();
        response.getOutputStream().close();
        out.close();
    }
}
